<?php

class transferTask extends sfBaseTask
{
  protected function configure()
  {
    // // add your own arguments here
    // $this->addArguments(array(
    //   new sfCommandArgument('my_arg', sfCommandArgument::REQUIRED, 'My argument'),
    // ));

    $this->addOptions(array(
      new sfCommandOption('application', null, sfCommandOption::PARAMETER_REQUIRED, 'The application name'),
      new sfCommandOption('env', null, sfCommandOption::PARAMETER_REQUIRED, 'The environment', 'dev'),
      new sfCommandOption('connection', null, sfCommandOption::PARAMETER_REQUIRED, 'The connection name', 'propel'),
      // add your own options here
      new sfCommandOption('limit', null, sfCommandOption::PARAMETER_REQUIRED, 'The limit for queries', '10'),
      new sfCommandOption('count', null, sfCommandOption::PARAMETER_NONE, 'Number of authors remaining to add'),
      new sfCommandOption('count-quotes', null, sfCommandOption::PARAMETER_NONE, 'Number of quotes in database'),
    ));

    $this->namespace        = 'authors';
    $this->name             = 'transfer';
    $this->briefDescription = 'Transfers authors from old db to the new one';
    $this->detailedDescription = <<<EOF
The [transfer|INFO] task does things.
Call it with:

  [php symfony transfer|INFO]
EOF;
  }

  protected function execute($arguments = array(), $options = array())
  {
    // initialize the database connection
    $databaseManager = new sfDatabaseManager($this->configuration);
    $connection = $databaseManager->getDatabase($options['connection'])->getConnection();
    $limit = $options['limit'];
    $total_quotes = 0;

    $link = mysql_connect('localhost', 'root', '123456');

    //Total time initialization
    $mtime = microtime();
    $mtime = explode(" ",$mtime);
    $mtime = $mtime[1] + $mtime[0];
    $starttime = $mtime;


    $query = "SELECT authors_final.id as id,
      authors_final.author as author,
      quotes.author.name as author_from_quote,
      authors_final.author_href,
      authors_final.letter as letter,
      authors_final.quote_pages as quote_pages
      FROM zend_pre.authors_final
      LEFT JOIN quotes.author ON zend_pre.authors_final.author = quotes.author.name
      WHERE quotes.author.name IS NULL
      limit $limit";

    if ($options['count'])
    {
      $query = "SELECT count(*) as count
        FROM zend_pre.authors_final
        LEFT JOIN quotes.author ON zend_pre.authors_final.author = quotes.author.name
        WHERE quotes.author.name IS NULL";

      $result = mysql_query($query);
      $row = mysql_fetch_array($result);
      $this->logSection('count', $row['count'].' remaining authors');
      exit;
    }

    if ($options['count-quotes'])
    {
      $count = QuoteQuery::create()->count();
      $this->logSection('count', $count.' quotes in database');
      exit;
    }

    $affected = 0;
    $result = mysql_query($query);
    while($row = mysql_fetch_array($result))
    {
      $affected++;
      //$this->logSection('author >>', $affected.'. '.$row['author']);

      //author time
      $mtime = microtime();
      $mtime = explode(" ",$mtime);
      $mtime = $mtime[1] + $mtime[0];
      $starttime_author = $mtime;

      $author_saved = true;
      $author = new Author();
      $author->setLetter($row['letter']);
      $author->setQuotePages($row['quote_pages']);
      $author->setIsActive(true);
      $author->setName($row['author']);
      try
      {
        $author->save();
      }
      catch(Exception $e)
      {
        $author_saved = false;
        $this->logBlock('author '.$row['author'].' failed to add!', 'error');
      }

      if($author_saved)
      {
        $que = "SELECT `quote` FROM `zend_pre`.`quotes` WHERE author_id = ".$row['id'];
        $res = mysql_query($que);
        $count = 0;
        while($r = mysql_fetch_array($res))
        {
          $quote = new Quote();
          $quote->setAuthorId($author->getId());
          $quote->setContent($r['quote']);
          $quote->setIsActive(true);
          $quote->save();
          $count++;
        }
      }
      //$this->logSection('quotes', $count.' quotes');

      //author end time
      $mtime = microtime();
      $mtime = explode(" ",$mtime);
      $mtime = $mtime[1] + $mtime[0];
      $endtime_author = $mtime;
      $totaltime_author = ($endtime_author - $starttime_author);
      $measure_author = 'seconds';
      if($totaltime_author > 60)
      {
        $totaltime_author/=60;
        $measure_author = 'minutes';
      }
      //$this->logSection('time', 'author took '.round($totaltime_author, 2).' '.$measure_author);
      $this->logSection('author >>', $affected.'. '.$row['author'].' with '.$count.' quotes in '.round($totaltime_author, 2).' '.$measure_author);
      $total_quotes+=$count;

    }
    $this->logSection('total affected', $affected.' authors added');
    $this->logSection('total affected', $total_quotes.' quotes added');


    $mtime = microtime();
    $mtime = explode(" ",$mtime);
    $mtime = $mtime[1] + $mtime[0];
    $endtime = $mtime;
    $totaltime = ($endtime - $starttime);
    $measure = 'seconds';
    if($totaltime > 60)
    {
      $totaltime/=60;
      $measure = 'minutes';
    }
    $this->logSection('total time', 'Task finished in '.round($totaltime, 2).' '.$measure);
    mysql_close($link);
  }
}
